package com.springmvc.controller;

import com.springmvc.model.Test;
import com.springmvc.model.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        //使用Shiro框架提供的方式进行认证
        Subject subject = SecurityUtils.getSubject(); //获得当前登录用户对象，现在状态为 “未认证”
        //用户名密码令牌
        AuthenticationToken token = new UsernamePasswordToken();
        try {
            subject.login(token); //执行你自定义的Realm
            User user1 = (User) subject.getPrincipal();
            session.setAttribute("loginUser",user1);
            return null;
        } catch (UnknownAccountException e) {
            e.printStackTrace();
        }
        return "test/testList";
    }
}